package com.example.task.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.example.task.R;
import com.example.task.bean.Lecture;
import com.example.task.bean.Notice;
import com.example.task.delegate.SimpleDelegateAdapter;
import com.example.task.http.Threads.AllActThreads;
import com.example.task.http.Threads.NoticeThread;
import com.example.task.http.Threads.SignUpBackNumByActIdThread;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.FormBody;

public class NoticesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notices);

        SimpleDelegateAdapter<Notice> mNewsAdapter;
        SmartRefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(NoticesActivity.this);
        recyclerView.setLayoutManager(layoutManager);

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("key","data");
        List<Notice> noticeList = getdata(builder);
        System.out.println("no"+noticeList);
        mNewsAdapter = new SimpleDelegateAdapter<Notice>(R.layout.activity_notice_item, new LinearLayoutHelper(),noticeList) {

            @Override
            protected void bindData(@NonNull RecyclerViewHolder holder, int position, Notice notice) {
                if (notice!= null) {
                    holder.text(R.id.name, notice.getUsername());
                    holder.text(R.id.time, notice.getDate());
                    holder.text(R.id.text,notice.getData());
                }
            }
        };
        recyclerView.setAdapter(mNewsAdapter);
        refreshLayout.setOnRefreshListener(refreshLayout1 -> refreshLayout1.getLayout().postDelayed(() -> {
            List<Notice> noticeList1 = getdata(builder);
            mNewsAdapter.refresh(noticeList1);
            refreshLayout1.finishRefresh();
        }, 1000));

    }
    private List<Notice> getdata(FormBody.Builder builder){
        NoticeThread thread = new NoticeThread(builder);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("公告"+thread.value);
        JsonArray obj = new Gson().fromJson(thread.value, JsonArray.class);

        List<Notice> noticeList = new ArrayList<>();
        for (JsonElement jsonElement : obj) {
            String username = jsonElement.getAsJsonObject().get("username").toString().replace("\"","");
            String data= jsonElement.getAsJsonObject().get("data").toString().replace("\"","");
            String idannouncement= jsonElement.getAsJsonObject().get("idannouncement").toString().replace("\"","");
            String date= jsonElement.getAsJsonObject().get("date").toString().replace("\"","");
            Notice notice = new Notice(username, data, idannouncement, date);
            noticeList.add(notice);
        }
        System.out.println(noticeList);
        Collections.reverse(noticeList);
        return noticeList;
    }
}